﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.Auth
{
    public partial class JueSeGuanLi : BaseForm
    {
        public JueSeGuanLi()
        {
            InitializeComponent();
        }

        private void JueSeGuanLi_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindInfo();
        }

        private void BindInfo()
        {
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_juese");
            if (Dt == null)
            {
                return;
            }
            dataGridView1.DataSource = Dt;

            dataGridView1.AutoResizeColumns();
        }

        private void menu_1_xinzeng_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("用户管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            JueSeBianJi jueSeBianJi = new JueSeBianJi();
            if (jueSeBianJi.ShowDialog() == DialogResult.OK)
            {
                BindInfo();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
        }

        private void menu_1_xiugai_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("用户管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            if (dataGridView1.SelectedRows.Count == 0)
            {
                return;
            }
            int selectIndex = dataGridView1.SelectedRows[0].Index;
            JueSeBianJi jueSeBianJi = new JueSeBianJi(dataGridView1.SelectedRows[0]);
            if (jueSeBianJi.ShowDialog() == DialogResult.OK)
            {
                BindInfo();
                dataGridView1.Rows[selectIndex].Selected = true;
            }
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        private void menu_1_shanchu_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("用户管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            if (dataGridView1.SelectedRows .Count ==0)
            {
                return;
            }
                if (KeaoUtils.ShowQuestionMessage("确定要删除该角色吗？\r\n删除后该角色下的所有用户将无法操作系统。") == DialogResult.Yes)
                {
                    if (KeaoUtils._DB.DeleteRowByID(new TableRow() { TableName = "tbl_juese", ID = dataGridView1.SelectedRows[0].Cells["col_id"].Value.ToString() })>0)
                    {
                        KeaoUtils.ShowInfoMessage("角色删除成功。");
                        BindInfo();
                    }
                }

        }
    }
}
